<table id="dg-rule" style="height:100%;width: 100%;">
    <thead>
    <tr>
        <th data-options="field:'id'">ID</th>
        <th data-options="field:'title',width:200">规则名</th>
        <th data-options="field:'name',align:'left',width:200">路径</th>
        <th data-options="field:'status',align:'center',formatter:luck.datagrid_status">状态</th>
        <th data-options="field:'showed',align:'center',formatter:luck.datagrid_showed">显示</th>
        <th data-options="field:'system_menu',align:'center',formatter:luck.datagrid_if">系统菜单</th>
        <th data-options="field:'operate',align:'center',formatter:rowoperate">操作</th>
    </tr>
    </thead>
</table>

<div id="tb-rule">
    <table cellpadding="0" cellspacing="0" style="width:100%">
        <tr class="clearfix">
            <td class="luck-tool-button luck-left">
                <a href="#" class="easyui-linkbutton button-blue" plain="true" onclick="rule_tool.add();"><i class="fa fa-plus fa-lg"></i> 添加</a>
                <a href="#" class="easyui-linkbutton button-teal" plain="true" onclick="rule_tool.edit();"><i class="fa fa-edit fa-lg"></i> 修改</a>
                <a href="#" class="easyui-linkbutton button-red" plain="true" onclick="rule_tool.remove();"><i class="fa fa-close fa-lg"></i> 删除</a>
            </td>
            <td class="luck-tool-button luck-left">
                <div class="datagrid-btn-separator"></div>
            </td>
            <td class="luck-tool-button luck-left">
                <a href="#" class="easyui-linkbutton button-black" plain="true" onclick="rule_tool.refresh();"><i class="fa fa-refresh fa-lg"></i> 刷新</a>
            </td>
        </tr>
    </table>
</div>

<script>
    var dg_rule=$('#dg-rule');
    $(function(){
        dg_rule.treegrid({
            url:"{:url('admin/rule/index')}",
            lines:true,
            method:'post',
            idField:'id',
            treeField:'title',
            nowrap:'false',
            fitColumns:false,
            rownumbers:true,
            sortName:'id',	//选择字段排序
            sortOrder:'asc',	//默认排序的方式 asc/desc
            pagination:false,	//开启分页
            //pageSize:PAGE_SIZE,	//默认分页数
            //pageList:PAGE_LIST,	//分页模式
            selectOnCheck:true,
            checkOnSelect:true,	//点击行选中
            singleSelect:true,	//多选
            loadMsg:'加载中...',
            striped:true,	//添加斑马线
            toolbar: '#tb-rule',
            emptyMsg:'<span class="font-pink">没有记录</span>',
        });
    });
    function rowoperate(val,row,index){
        return '<a href="#" class="easyui-linkbutton button-teal luck-row-small" plain="true" onclick="rule_tool.edit('+row.id+');"><i class="fa fa-edit fa-lg"></i> 修改</a> ' +
                '<a href="#" class="easyui-linkbutton button-red luck-row-small" plain="true" onclick="rule_tool.remove('+row.id+');"><i class="fa fa-close fa-lg"></i> 删除</a>';
    }
    var rule_tool={
        add:function(){
            $('#add-dialog').dialog({
                title: '添加规则',
                width:500,
                height:550,
                closed: false,
                cache: false,
                cls:'theme-panel-deepblue luck-dialog-buttons',
                modal: true,
                href:"{:url('admin/rule/add')}",
                buttons:[{
                    cls:'easyui-linkbutton button-blue',
                    iconCls:'fa fa-check fa-lg',
                    text:'保存',
                    handler:function(){
                        rule_tool.add_save();
                    }
                },{
                    iconCls:'fa fa-close fa-lg',
                    text:'关闭',
                    handler:function(){
                        luck.closeDialog();
                    }
                }],
                fit: false,
                onBeforeOpen:function(){
                    $('#add-dialog').window('center');
                }
            });
            //清除关闭数据没清空问题
            $("#form-add").form('clear');
        },
        edit:function(id){
            var rows=dg_rule.treegrid('getSelections');
            if(rows.length==1 || id){
                $('#add-dialog').dialog({
                    title: '修改规则',
                    width:500,
                    height:550,
                    closed: false,
                    cache: false,
                    cls:'theme-panel-deepblue luck-dialog-buttons',
                    modal: true,
                    href:"{:url('admin/rule/edit')}",
                    queryParams: {
                        id:id?id:rows[0].id,
                    },
                    buttons:[{
                        cls:'easyui-linkbutton button-blue',
                        iconCls:'fa fa-check fa-lg',
                        text:'保存',
                        handler:function(){
                            rule_tool.edit_save();
                        }
                    },{
                        iconCls:'fa fa-close fa-lg',
                        text:'关闭',
                        handler:function(){
                            luck.closeDialog();
                        }
                    }],
                    fit: false,
                    onBeforeOpen:function(){
                        $('#add-dialog').window('center');
                    }
                });
            }else{
                $.messager.alert('警告', '编辑数据需要操作一条,且最多只能操作一条!!', 'warning');
            }
        },
        remove:function(id){
            var rows=dg_rule.treegrid('getSelections');
            if(rows.length>0 || id){
                var ids=[];
                var confirmmsg='您要删除所选的记录吗？';
                if(id){
                    ids.push(id);
                    confirmmsg='您要删除这一条记录吗？';
                }else{
                    for(var i=0;i<rows.length;i++){
                        ids.push(rows[i].id);
                    }
                }

                $.messager.confirm('确定操作', confirmmsg, function (flag) {
                    if(flag){

                        $.ajax({
                            type:"post",
                            url:'{:url("admin/rule/remove")}',
                            data:{
                                id:ids,
                            },
                            beforeSend : function () {
                                dg_rule.treegrid('loading');
                            },
                            success : function (data, response, status) {
                                luck.successtree2(data,dg_rule);
                            },
                            complete:function(){
                                dg_rule.treegrid('loaded');
                            },
                            error : function(){

                            }
                        });
                    }
                });
            }else{
                $.messager.alert('警告', '删除数据需要操作至少一条!!', 'warning');
            }
        },
        refresh:function(){
            dg_rule.treegrid('reload');
            dg_rule.treegrid('clearSelections');
            //dg.treegrid('load');
        },
        add_save:function(){
            $("#form-add").form('submit',{
                url:"{:url('admin/rule/add')}",
                onSubmit:function(){
                    var isValid = $(this).form('validate');
                    isValid?$.messager.progress():"";
                    return isValid;
                },
                success:function(data){
                    luck.successtree(data,dg_rule);
                }
            })
        },
        edit_save:function(){
            $("#form-add").form('submit',{
                url:"{:url('admin/rule/edit')}",
                onSubmit:function(){
                    var isValid = $(this).form('validate');
                    isValid?$.messager.progress():"";
                    return isValid;
                },
                success:function(data){
                    luck.successtree(data,dg_rule);
                }
            })
        },
        search:function(){
            dg_rule.treegrid('load',{
                'search_username': $('input[name=title]').val()
            });
        }
    }
</script>